package cc.fozone.note.view;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import cc.fozone.note.configuration.IConfiguration;
import cc.fozone.note.help.DesktopHelper;


public class AboutView extends BorderPane {
	private IConfiguration<String,String> configuration;
	
	private String name;
	private String version;
	private String author;
	private String homepage;
	private String weibo;
	private String email;
	private String github;
	
	private String appStyle = "app.css";
	private String appClass = "about";
	private String nameClass = "about-name";
	private String versionClass = "about-version";
	private String authorClass = "about-author";
	private String homepageClass = "about-homepage";
	private String weiboClass = "about-weibo";
	private String emailClass = "about-email";
	private String githubClass = "about-github";
	private String logoClass = "about-logo";
	
	private Label nameLabel,versionLabel,authorLabel;
	private Hyperlink homepageLink, weiboLink, emailLink, githubLink;
	
	public AboutView(IConfiguration<String,String> configuration){
		this.configuration = configuration;
		this.init();
		this.handle();
	}
	
	private void init(){
		this.buildConfiguration();
		this.buildView();
		
	}
	
	private void buildConfiguration() {
		name = configuration.get("app.name","Note.FO");
		version =configuration.get("app.version","");
		author = configuration.get("app.author","41ZONE");
		
		homepage = configuration.get("app.homepage","http://note.41zone.cc");
		weibo = configuration.get("app.weibo","http://weibo.com/41zone");
		email = configuration.get("app.email","foer@41zone.cc");
		github = configuration.get("app.git","http://github.com/41zone");
		
		appClass = configuration.get("app.about.styleClass",appClass);
		nameClass = configuration.get("app.about.nameStyleClass",nameClass);
		authorClass = configuration.get("app.about.authorStyleClass",authorClass);
		homepageClass = configuration.get("app.about.homepageStyleClass",homepageClass);
		weiboClass = configuration.get("app.about.weiboStyleClass",weiboClass);
		emailClass = configuration.get("app.about.emailStyleClass",emailClass);
		githubClass = configuration.get("app.about.githubStyleClass",githubClass);
		logoClass = configuration.get("app.about.logoStyleClass",logoClass);
		appStyle = configuration.get("app.style",appStyle);
	}
	
	private void buildView(){
		ImageView logo = new ImageView();
		logo.getStyleClass().addAll(logoClass);
		
		nameLabel = new Label(name);
		nameLabel.getStyleClass().addAll(nameClass);
		
		versionLabel = new Label(version, new Label("version"));
		versionLabel.getStyleClass().addAll(versionClass);
		
		authorLabel = new Label(author,new Label("Developer   : "));
		authorLabel.getStyleClass().addAll(authorClass);
		
		homepageLink = new Hyperlink(homepage,new Label("HomePage : "));
		homepageLink.getStyleClass().addAll(homepageClass);
		
		weiboLink = new Hyperlink(weibo,new Label("Weibo : "));
		weiboLink.getStyleClass().addAll(weiboClass);
		
		emailLink = new Hyperlink(email,new Label("Email : "));
		emailLink.getStyleClass().addAll(emailClass);
		
		githubLink = new Hyperlink(github, new Label("GITHUB : "));
		githubLink.getStyleClass().addAll(githubClass);
		
		AnchorPane pane = new AnchorPane();
		
		/*
		,versionLabel,authorLabel,
		homepageLink,weiboLink,
		emailLink,githubLink
		*/
		pane.getChildren().addAll(
			nameLabel,versionLabel,authorLabel,
			homepageLink,githubLink,weiboLink,emailLink
		);
		
		AnchorPane.setLeftAnchor(nameLabel, Double.valueOf(20));
		
		AnchorPane.setLeftAnchor(versionLabel, Double.valueOf(130));
		AnchorPane.setTopAnchor(versionLabel, Double.valueOf(10));
		
		AnchorPane.setLeftAnchor(authorLabel, Double.valueOf(44));
		AnchorPane.setTopAnchor(authorLabel, Double.valueOf(44));
		
		AnchorPane.setLeftAnchor(homepageLink, Double.valueOf(44));
		AnchorPane.setTopAnchor(homepageLink, Double.valueOf(64));

		AnchorPane.setLeftAnchor(githubLink, Double.valueOf(44));
		AnchorPane.setTopAnchor(githubLink, Double.valueOf(94));
		
		AnchorPane.setLeftAnchor(weiboLink, Double.valueOf(44));
		AnchorPane.setTopAnchor(weiboLink, Double.valueOf(114));
		
		AnchorPane.setLeftAnchor(emailLink, Double.valueOf(44));
		AnchorPane.setTopAnchor(emailLink, Double.valueOf(134));
		
		this.setLeft(logo);
		this.setCenter(pane);
		this.getStyleClass().addAll(appClass);
		this.getStylesheets().add(appStyle);
	}
	
	private void handle(){
		HyperlinkEventHandler handler = new HyperlinkEventHandler();
		
		homepageLink.setOnAction(handler);
		weiboLink.setOnAction(handler);
		githubLink.setOnAction(handler);
		emailLink.setOnAction(arg0 -> {
            // TODO Auto-generated method stub
            DesktopHelper.mail(emailLink.getText());
        });
	}
	
	private class HyperlinkEventHandler implements EventHandler<ActionEvent> {

		@Override
		public void handle(ActionEvent event) {
			// TODO Auto-generated method stub
			Hyperlink link = (Hyperlink) event.getSource();
			if(link == null) return ;
			DesktopHelper.browse(link.getText());
		}
		
	}
	
}
